# -*- coding: utf-8 -*-
# Created by 'Zhou Bingbing'  on 2019/8/23
import codecs
import time
import numpy
from config_setting.config_setting import DB
import os
PATH=os.path.abspath(os.path.dirname(__file__))
class ProcessData:
    @staticmethod
    def read_file(file_path):
        try:
            f = codecs.open(file_path, "r", "GBK")
            lines = f.readlines()
        except:
            f = codecs.open(file_path, "rb", "utf-8",'ignore')
            lines=f.readlines()
        for line in lines:
            content= ' '.join(line.split(' ')[1:])
            DB.sys_cilin.update({'content': content}, {'$set': {'time': str(time.ctime())}}, True)

    @staticmethod
    def save_all_vocb():
        f = codecs.open('../sys_cilin/new_cilin.txt', 'r', "GBK")
        lines = f.readlines()
        vacb = {}
        for line in lines:
            line = line.strip()
            keys = line.split(' ')[0]
            values = line.split(' ')[1:]
            vacb[keys] = values
        numpy.savez(PATH+'/vacb_all', dict=vacb)
    @staticmethod
    def load_cilin_all():
        arrays = numpy.load(PATH+'/vacb_all.npz')
        return arrays['dict'][()]
    @staticmethod
    def get_index_vocab_keys(get_value,mydisc):
        key_list = []
        value_lists = []
        for key, value in mydisc.items():
            key_list.append(key)
            value_lists.append(value)
        res=[]
        for value_list in value_lists:
            if get_value in value_list:
                get_value_index = value_lists.index(value_list)
                res.append( key_list[get_value_index])
            else: #不存在我们的词汇表格里面
                continue
        return res
    @staticmethod
    def dereplication(wordlist):
        return list(set(wordlist))
def main(word):
    mydisc=ProcessData.load_cilin_all()
    result=ProcessData.get_index_vocab_keys(word, mydisc)
    words_similar=[]
    if result:
        for index in result:
            words_similar.extend(mydisc[index])
            words_similar=ProcessData.dereplication(words_similar)
    else:
        words_similar.append(word)
    return words_similar
# f = codecs.open('../sys_cilin/new_cilin.txt', 'r', "GBK")
# lines = f.readlines()
# vacb = {}
# for line in lines:
#     line = line.strip()
#     keys = line.split(' ')[0]
#     values = line.split(' ')[1:]
#     vacb[keys] = values
# numpy.savez(PATH+'/vacb_all', dict=vacb)